package com.sc.nft.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.entity.ReportAmount;
import com.sc.nft.entity.dto.ReportSaveDTO;
import com.sc.nft.entity.vo.ReportAmountVO;
import com.sc.nft.entity.vo.TotalReportAmountVO;
import com.sc.nft.enums.StatisticalStatementEnum;
import com.sc.nft.helper.ReportOverviewHelper;
import com.sc.nft.service.ReportAmountService;
import com.sc.nft.service.ReportOverviewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务接口实现
 *
 * @author Sun
 * @since 2023-02-20 14:10:29
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ReportAmountServiceImpl implements ReportAmountService {
    private final ReportAmountDao reportAmountDao;
    private final DepositWithdrawalRecordDao depositWithdrawalRecordDao;
    private final SandTransferRecordDao sandTransferRecordDao;
    private final SandWithdrawRecordDao sandWithdrawRecordDao;
    private final OrderDao orderDao;
    private final SecondOrderDao secondOrderDao;
    private final PayOrderDao payOrderDao;
    private final UserIncomeRecordDao userIncomeRecordDao;
    private final UserWalletDetailDao userWalletDetailDao;
    private final ReportOverviewService reportOverviewService;

    /**
     *     充值金额：单日用户充值金额总和
     *     转账金额：单日审核通过的金额总和(平台转钱包)，数值取：【用户转账金额】
     *     提现金额：单日成功提现的金额总和(钱包转银行卡)，数值取：【用户提现金额】
     *     首发交易额：单日一级市场交易额，数值取：【订单购买金额】
     *     二级交易额：单日二级市场交易额(含孵化和自由)，数值取：【订单购买金额】
     *     手续费：单日产生的第三方手续费总和 (转账手续费 + 提现手续费 + 银行卡支付手续费）
     *     现金奖励拨出：单日已发放的现金奖励金额
     *     *奖励拨出=推广奖励+特殊奖励+城市服务中心+财务加款+领主分红
     *     *推广奖励 = 直推+间推(含一级和二级）
     *     *特殊奖励 = 团队贡献奖 + 培育奖（含一级和二级）
     *     *城市服务中心 = 首发分红+寄售分红+IP分红+市场补贴+平台市值收益+平台利润收益
     *     *财务加款 = 加款[选取提现余额]
     *     红包奖励拨出：单日已发放的红包奖励金额
     *     城主合成红包+城主购买红包+居民卡合成红包+居民卡购买红包+手续费返现红包+财务加款[支付余额]
      */
    @Override
    public void insertReportAmount() {


        ReportAmount reportAmount = new ReportAmount();
        BigDecimal toDayRechargeAmount = depositWithdrawalRecordDao.getToDayRechargeAmount();
        reportAmount.setToDayRechargeAmount(toDayRechargeAmount);
        reportAmount.setToDayTransferAmount(sandTransferRecordDao.getToDayTransferAmount());
        BigDecimal toDayWithdrawAmount = sandWithdrawRecordDao.getToDayWithdrawAmount();
        reportAmount.setToDayWithdrawAmount(toDayWithdrawAmount);
        log.info("-----------------------------------------------------------------");
        log.info(DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd HH:mm:ss") + " 的提现金额为: " +toDayWithdrawAmount.stripTrailingZeros().toPlainString());
        log.info(DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd HH:mm:ss") + " 的提现金额为: " +toDayWithdrawAmount.stripTrailingZeros().toPlainString());
        log.info(DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd HH:mm:ss") + " 的提现金额为: " +toDayWithdrawAmount.stripTrailingZeros().toPlainString());
        log.info("-----------------------------------------------------------------");
        reportAmount.setToDaySellAmount(orderDao.getToDaySellAmount());
        reportAmount.setToDaySecondSellAmount(secondOrderDao.getToDaySecondSellAmount());

        //转账手续费
        BigDecimal toDayTransferCharge = sandTransferRecordDao.getToDayTransferCharge();
        //提现手续费
        BigDecimal toSumPayAmount = payOrderDao.getToSumPayAmount();
        //  (一级市场支付总额 + 充值金额) * 千6  = 支付手续费
        //  支付手续费 + 转账手续费 = 手续费
        BigDecimal toDayCharge = toSumPayAmount.add(toDayRechargeAmount).multiply(new BigDecimal("0.006"));
        reportAmount.setToDayCharge(toDayCharge.add(toDayTransferCharge));

        BigDecimal toDayIncomeNum = userIncomeRecordDao.getToDayIncomeNum();
        BigDecimal toDayBackstageAddByBalance = userWalletDetailDao.getToDayBackstageAddByBalance();
        reportAmount.setToDayIncomeNum(toDayIncomeNum.add(toDayBackstageAddByBalance));

        // 城主合成红包+城主购买红包+居民卡合成红包+居民卡购买红包 (type = 10)
        // +手续费返现红包(type = 9)
        // +财务加款[支付余额](type = 6)
        reportAmount.setToDayIncomePayBalanceNum(userWalletDetailDao.getToDayIncomePayBalanceNum());
        reportAmount.insert();
    }

    @Override
    public void insertReportAmountNewEdition() {

        List<ReportSaveDTO> list = new ArrayList<>();

        BigDecimal toDayRechargeAmount = depositWithdrawalRecordDao.getToDayRechargeAmount();
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_AMOUNT_RECHARGED_BY_USERS_IN_ONE_DAY,  toDayRechargeAmount);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_AMOUNT_OF_TRANSFERS_APPROVED_IN_A_SINGLE_DAY, sandTransferRecordDao.getToDayTransferAmount());
        BigDecimal toDayWithdrawAmount = sandWithdrawRecordDao.getToDayWithdrawAmount();
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_AMOUNT_SUCCESSFULLY_WITHDRAWN_IN_A_SINGLE_DAY,  sandWithdrawRecordDao.getToDayWithdrawAmount());
        log.info("-----------------------------------------------------------------");
        log.info(DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd HH:mm:ss") + " 的提现金额为: " +toDayWithdrawAmount.stripTrailingZeros().toPlainString());
        log.info(DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd HH:mm:ss") + " 的提现金额为: " +toDayWithdrawAmount.stripTrailingZeros().toPlainString());
        log.info(DateUtil.format(DateUtil.yesterday(),"yyyy-MM-dd HH:mm:ss") + " 的提现金额为: " +toDayWithdrawAmount.stripTrailingZeros().toPlainString());
        log.info("-----------------------------------------------------------------");

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SINGLE_DAY_PRIMARY_MARKET_TURNOVER,  orderDao.getToDaySellAmount());

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SINGLE_DAY_SECONDARY_MARKET_TURNOVER, secondOrderDao.getToDaySecondSellAmount());
        //转账手续费
        BigDecimal toDayTransferCharge = sandTransferRecordDao.getToDayTransferCharge();
        //提现手续费
        BigDecimal toSumPayAmount = payOrderDao.getToSumPayAmount();
        //  (一级市场支付总额 + 充值金额) * 千6  = 支付手续费
        //  支付手续费 + 转账手续费 = 手续费
        BigDecimal toDayCharge = toSumPayAmount.add(toDayRechargeAmount).multiply(new BigDecimal("0.006"));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_THIRD_PARTY_FEES_GENERATED_IN_A_SINGLE_DAY, toDayCharge.add(toDayTransferCharge));
        BigDecimal toDayIncomeNum = userIncomeRecordDao.getToDayIncomeNum();
        BigDecimal toDayBackstageAddByBalance = userWalletDetailDao.getToDayBackstageAddByBalance();

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.THE_AMOUNT_OF_CASH_AWARDED_IN_A_SINGLE_DAY,  toDayIncomeNum.add(toDayBackstageAddByBalance));
        // 城主合成红包+城主购买红包+居民卡合成红包+居民卡购买红包 (type = 10)
        // +手续费返现红包(type = 9)
        // +财务加款[支付余额](type = 6)

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.THE_AMOUNT_OF_RED_PACKETS_AWARDED_IN_A_SINGLE_DAY, userWalletDetailDao.getToDayIncomePayBalanceNum());
        reportOverviewService.saveBath(list);
    }

//    @Override
//    public void insertReportAmountByDay(String day) throws ParseException {
//        ReportAmount reportAmount = new ReportAmount();
//        BigDecimal toDayRechargeAmount = depositWithdrawalRecordDao.getToDayRechargeAmountByDay(day);
//        reportAmount.setToDayRechargeAmount(toDayRechargeAmount);
//        reportAmount.setToDayTransferAmount(sandTransferRecordDao.getToDayTransferAmountByDay(day));
//        reportAmount.setToDayWithdrawAmount(sandWithdrawRecordDao.getToDayWithdrawAmountByDay(day));
//        reportAmount.setToDaySellAmount(orderDao.getToDaySellAmountByDay(day));
//        reportAmount.setToDaySecondSellAmount(secondOrderDao.getToDaySecondSellAmountByDay(day));
//
//        //转账手续费
//        BigDecimal toDayTransferCharge = sandTransferRecordDao.getToDayTransferChargeByDay(day);
//        //提现手续费
//        BigDecimal toSumPayAmount = payOrderDao.getToSumPayAmountByDay(day);
//        //  (一级市场支付总额 + 充值金额) * 千6  = 支付手续费
//        //  支付手续费 + 转账手续费 = 手续费
//        BigDecimal toDayCharge = toSumPayAmount.add(toDayRechargeAmount).multiply(new BigDecimal("0.006"));
//        reportAmount.setToDayCharge(toDayCharge.add(toDayTransferCharge));
//
//        BigDecimal toDayIncomeNum = userIncomeRecordDao.getToDayIncomeNumByDay(day);
//        BigDecimal toDayBackstageAddByBalance = userWalletDetailDao.getToDayBackstageAddByBalanceByDay(day);
//        reportAmount.setToDayIncomeNum(toDayIncomeNum.add(toDayBackstageAddByBalance));
//
//        // 城主合成红包+城主购买红包+居民卡合成红包+居民卡购买红包 (type = 10)
//        // +手续费返现红包(type = 9)
//        // +财务加款[支付余额](type = 6)
//        reportAmount.setToDayIncomePayBalanceNum(userWalletDetailDao.getToDayIncomePayBalanceNumByDay(day));
//        reportAmount.insert();
//
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        reportAmount.setCreateTime(sdf.parse(day));
//        reportAmount.updateById();
//    }

    @Override
    public Page<ReportAmountVO> getPageReportAmount(String beginTime, String endTime, Integer pageNo, Integer pageSize) {
        return reportAmountDao.getPageReportAmount(beginTime,endTime,pageNo,pageSize);
    }

    @Override
    public TotalReportAmountVO getTotalReportAmount(String beginTime, String endTime) {
        return reportAmountDao.getTotalReportAmount(beginTime,endTime);
    }
}